package com.zrx.govern.component.easyexcel.starter.feign.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * feign请求token拦截器
 * @author zhangxiaowei
 * @version V1.0
 **/
public class TokenRelayRequestIntecepor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {

        try {
            // 1. 获取到token
            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
            ServletRequestAttributes attributes = (ServletRequestAttributes) requestAttributes;
            if(attributes!=null){
                HttpServletRequest request = attributes.getRequest();
                String  token = request.getHeader("Authorization");
                // 2. 将token传递
                if (!StringUtils.isEmpty(token)) {
                    template.header("Authorization", token);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }


    }
}
